package com.atguigu.query;

import org.apache.http.HttpHost;
import org.apache.lucene.analysis.path.PathHierarchyTokenizer;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * create by undeRdoG on  2021-06-21  22:56
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class ESTest_Doc_Query {

    public static void main(String[] args) throws IOException {
        //  创建ES 客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200))
        );

       /* // 查询数据

        SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");

        //  matchAllQuery  查询索引中全部的数据
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        request.source(query);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        // 获取查询结果
        SearchHits hits = response.getHits();

        System.out.println(hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }
*/


        //  2 条件查询

/*        SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");

        // 查询年龄为30
        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30 )));

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }*/


        /**
         *  分页查询
         *
         * */

/*        SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");

        //  matchAllQuery  查询索引中全部的数据
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        query.from(1);
        query.size(2);
        request.source(query);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }*/


        /*
         * 排序
         * */
/*
        SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");

        //  matchAllQuery  查询索引中全部的数据
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        // 根据年龄降序
        query.sort("age", SortOrder.DESC);
        request.source(query);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }*/



        /*
         * 展示部分字段
         * */

    /*    SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");

        //  matchAllQuery  查询索引中全部的数据
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

       // 展示部分字段
        String[] include = {"name"};
        String[] exclude = {};
        query.fetchSource(include,exclude);
        request.source(query);


        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }*/



      /**
      *
      *  模糊查询
      * */


       /* {
            "_index" : "user",
                "_type" : "_doc",
                "_id" : "1003",
                "_score" : 0.0,
                "_source" : {
            "name" : "王五",
                    "age" : 40,
                    "sex" : "男"
        }
        }
        {
            "_index" : "user",
                "_type" : "_doc",
                "_id" : "1004",
                "_score" : 0.0,
                "_source" : {
            "name" : "王五1",
                    "age" : 40,
                    "sex" : "女"
        }
        }
        {
            "_index" : "user",
                "_type" : "_doc",
                "_id" : "1005",
                "_score" : 0.0,
                "_source" : {
            "name" : "王五2",
                    "age" : 50,
                    "sex" : "男"
        }
        }
        {
            "_index" : "user",
                "_type" : "_doc",
                "_id" : "1006",
                "_score" : 0.0,
                "_source" : {
            "name" : "王五3",
                    "age" : 50,
                    "sex" : "男"
        }
        }*/

      /*  SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        //  这里模糊匹配，指差一个字符的数据能够匹配的到
        builder.query(QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE));
        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit);
        }*/



      /**
      *  聚合查询
      *
      * */

        SearchRequest request = new SearchRequest();
        //  指定查询索引
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        // 去年龄的最大值
        AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxGAE").field("age");
        builder.aggregation(aggregationBuilder);

        request.source(builder);

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        esClient.close();
    }
}
